package com.jz.community.moduleshopping.shopCart.adapter;

import android.app.Activity;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jz.community.basecomm.bean.BaseResponseInfo;
import com.jz.community.basecomm.constant.AppConstants;
import com.jz.community.basecomm.constant.ConstantErrorCode;
import com.jz.community.basecomm.task.ITaskCallbackListener;
import com.jz.community.basecomm.task.UpdateCartGoodsNumTask;
import com.jz.community.basecomm.utils.BaseImageLoaderUtils;
import com.jz.community.basecomm.utils.CharacterUtils;
import com.jz.community.basecomm.utils.ConverterUtils;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.basecomm.utils.RxTextTool;
import com.jz.community.basecomm.utils.SHelper;
import com.jz.community.basecomm.utils.WpToast;
import com.jz.community.moduleshopping.R;
import com.jz.community.moduleshopping.shopCart.bean.CartGoodInfo;

import java.util.List;

/**
 * 有效购物列表
 */
public class ValidCartItemAdapter extends BaseQuickAdapter<CartGoodInfo, BaseViewHolder> {

    private CheckItemListener checkItemListener;

    public void setCheckItemListener(CheckItemListener checkItemListener) {
        this.checkItemListener = checkItemListener;
    }

    public ValidCartItemAdapter(@Nullable List<CartGoodInfo> data) {
        super(R.layout.module_shopping_item_cart_valid_item_layout, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, CartGoodInfo item) {
        showGoodsInfo(helper, item);
    }

    /**
     * 显示商品信息
     *
     * @param helper
     * @param cartInfosBean
     */
    private void showGoodsInfo(BaseViewHolder helper, CartGoodInfo cartInfosBean) {
        TextView cartItemPriceDisPriceTv = helper.getView(R.id.cart_item_price_disPrice_tv);
        helper.setText(R.id.cart_item_name, cartInfosBean.getGoodsName());
        helper.setText(R.id.cart_item_nums, cartInfosBean.getNum() + "");
        helper.setText(R.id.cart_item_spec, cartInfosBean.getSkuName());
        if (!Preconditions.isNullOrEmpty(cartInfosBean.getDiscountPrice())) {
            helper.setText(R.id.cart_item_price, AppConstants.MONEY + CharacterUtils.roundByGoodPrice(ConverterUtils.toDouble(cartInfosBean.getDiscountPrice())));
            RxTextTool.getBuilder("").append(AppConstants.MONEY + CharacterUtils.roundByGoodPrice(ConverterUtils.toDouble(cartInfosBean.getPrice()))).setStrikethrough().into(cartItemPriceDisPriceTv);
            SHelper.vis(cartItemPriceDisPriceTv);
        } else {
            helper.setText(R.id.cart_item_price, AppConstants.MONEY + CharacterUtils.roundByGoodPrice(ConverterUtils.toDouble(cartInfosBean.getPrice())));
            SHelper.gone(cartItemPriceDisPriceTv);
        }
        BaseImageLoaderUtils.getInstance().loadDefaltImage(mContext, helper.getView(R.id.cart_item_img), cartInfosBean.getIcon());
        addListener(helper, cartInfosBean, helper.getAdapterPosition());
        changeCheck(helper, helper.getAdapterPosition());
    }

    /**
     * 更新选中是否
     *
     * @param helper
     * @param position
     */
    private void changeCheck(BaseViewHolder helper, int position) {

        CheckBox cartItemCheck = helper.getView(R.id.cart_item_check);
        cartItemCheck.setChecked("Y".equalsIgnoreCase(getItem(position).getChecked()) ? true : false);

        cartItemCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkItemListener.isCheckItem(position, "Y".equalsIgnoreCase(getItem(position).getChecked()) ? false : true);
            }
        });
    }

    private void addListener(BaseViewHolder helper, CartGoodInfo cartInfosBean, int position) {
        TextView cartItemNumTv = helper.getView(R.id.cart_item_nums);
        ImageView cartItemAdd = helper.getView(R.id.cart_item_add);
        ImageView cartItemMinus = helper.getView(R.id.cart_item_minus);

        cartItemAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handleCartAddNumAndMinusNum(cartInfosBean, cartItemNumTv, position, true);
            }
        });

        cartItemMinus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handleCartAddNumAndMinusNum(cartInfosBean, cartItemNumTv, position, false);
            }
        });
    }

    /**
     * 购物数量 +  -
     *
     * @param cartItemNumTv
     * @param position
     * @param isAddNum
     */
    private void handleCartAddNumAndMinusNum(CartGoodInfo
                                                     cartInfosBean, TextView cartItemNumTv, int position, boolean isAddNum) {
        int num = Integer.parseInt(cartItemNumTv.getText().toString().trim());
        if (isAddNum) {
            getCartGoodsNumAndSkuTask(cartInfosBean.getId() + "", cartItemNumTv, (num + 1), position, isAddNum);
        } else {
            if (num > 1) {
                getCartGoodsNumAndSkuTask(cartInfosBean.getId() + "", cartItemNumTv, (num - 1), position, isAddNum);
            } else {
                WpToast.l(mContext, "已经是最小数量啦");
            }
        }
    }

    /**
     * 获取更新购物车数量、库存
     *
     * @param position
     */
    public void getCartGoodsNumAndSkuTask(String id, TextView cartItemNumTv, int carNum,
                                          int position, boolean isAddNum) {

        new UpdateCartGoodsNumTask((Activity) mContext, new ITaskCallbackListener() {
            @Override
            public void doTaskComplete(Object o) {
                BaseResponseInfo baseResponseInfo = (BaseResponseInfo) o;
                if (!Preconditions.isNullOrEmpty(baseResponseInfo)) {
                    if (baseResponseInfo.getCode() == ConstantErrorCode.SUCC_CODE) {
                        handleGoodsSkuUI(cartItemNumTv, position, isAddNum);
                    } else {
                        if (!Preconditions.isNullOrEmpty(baseResponseInfo.getMessage())) {
                            WpToast.l(mContext, baseResponseInfo.getMessage());
                        }
                    }
                }
            }
        }).execute(id, carNum + "");
    }

    /**
     * 商品库存
     *
     * @param cartItemNumTv
     * @param position
     * @param isAddNum
     */
    private void handleGoodsSkuUI(TextView cartItemNumTv, int position, boolean isAddNum) {
        int buyNum = ConverterUtils.toInt(cartItemNumTv.getText().toString().trim());
        if (isAddNum) {
            cartItemNumTv.setText((ConverterUtils.toString(buyNum + 1)));
        } else {
            cartItemNumTv.setText((ConverterUtils.toString(buyNum - 1)));
        }
        checkItemListener.isUpdateCheckItemNum(position, ConverterUtils.toInt(cartItemNumTv.getText().toString().trim()));
    }


    interface CheckItemListener {
        void isCheckItem(int position, boolean isChecked);

        void isUpdateCheckItemNum(int position, int num);
    }
}
